<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="materialCode" style="display: flex;">
				<span style="margin-right: 20rpx;">物料码</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.materialCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;">
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="materialBatch" style="display: flex;">
				<span style="margin-right: 20rpx;">批次号</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.materialBatch"
					@confirm="getDetail()"
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;">
				</u--input>
			</u-form-item>
			<u-form-item style="display: flex;">
				<view style="display: flex;flex: 1;align-items: center;">
					<span style="margin-right: 50rpx;">容器</span>
					<u-tag v-if="containerType === 1" style="margin-right: 50rpx;" text="台车" type="warning" plain plainFill size="medium"></u-tag>
					<u-tag v-else-if="containerType === 2" style="margin-right: 50rpx;" text="托盘" type="success" plain plainFill size="medium"></u-tag>
					<u-tag v-else-if="containerType === 3" style="margin-right: 50rpx;" text="工装" type="primary" plain plainFill size="medium"></u-tag>
					<u-tag v-else style="margin-right: 50rpx;" text="无" type="error" plain plainFill size="medium"></u-tag>
				</view>
				<view style="display: flex;flex: 1;align-items: center;">
					<span style="margin-right: 50rpx;">容器位置</span>
					<u-tag :text="getArea(area)" plain plainFill type="info"></u-tag>
				</view>
			</u-form-item>
			<u-form-item label="" prop="newContaner" style="display: flex;">
				<span style="margin-right: 20rpx;">容器码</span>
				<u--input
					placeholder="请输入/扫描"
					v-model="query.containerCode"
					@confirm="getDetail()"
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					:disabled="containerType === 4"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="quantity" style="display: flex;">
				<span style="margin-right: 50rpx;">数量</span>
				<u--input 
					placeholder="请输入数量" 
					v-model="query.quantity" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;">
				</u--input>
			</u-form-item>
		</u--form>
		<view style="display: flex;flex-direction: column;gap: 10rpx;margin-top: 10rpx;">
			<view style="display: flex;">
				<span style="flex: 1;">已入库数量</span>
				<span style="flex: 3;">{{ tips.realQty }}</span>	
			</view>
			<view style="display: flex;">
				<span style="flex: 1;">入库单数量</span>
				<span style="flex: 3;">{{ tips.InboundOrderQty }}</span>
			</view>
			<view style="display: flex;">
				<span style="flex: 1;">目标储位</span>
				<span style="flex: 3;">{{ tips.targetStorage || '无'}}</span>
			</view>
		</view>
		<view style="display: flex; flex-direction: column;margin-top: 10rpx;gap: 10rpx;" v-if="tableData.some(item => item.preQty !== 0)">
			<u-gap height="5" bgColor="#909399"></u-gap>
			<view style="flex: 1;">推荐放入物料：</view>
			<view style="display: flex; justify-content: space-between;">
				<view style="flex: 1;">物料号</view>
				<view style="flex: 1;">批次号</view>
				<view style="flex: 1;">数量</view>
			</view>
			<view
				style="display: flex; justify-content: space-between;"
				v-for="(item, index) in tableData"
				:key="index"
				v-if="item.preQty !== 0"
			>
				<view style="flex: 1;">{{ item.materialCode }}</view>
				<view style="flex: 1;">{{ item.materialBatch }}</view>
				<view style="flex: 1;">{{ item.preQty }}</view>
			</view>
		</view>
		<view style="position: fixed; bottom: 0; left: 0; width: 100%; padding: 10rpx; background-color: #fff; display: flex; justify-content: space-between;box-sizing: border-box;gap: 20rpx;">
			<u-button type="primary" text="确认" @click="submit"></u-button>
			<u-button type="primary" text="满车" @click="fullLoad"></u-button>
			<u-button type="warning" text="重置" @click="reset"></u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-modal
			:show="show"
			:title="title"
			:content='content'
			cancelText="取消"
			:showCancelButton="true"
			@cancel="show = false"
			@confirm="show = false"
		>
		</u-modal>
	</view>
</template>

<script>
	import { getDetailAPI, getMoreInfoAPI, bindAPI, fullLoadAPI, getNullPalletAPI } from '@/api/work/inbound.js'
	export default {
		data() {
			return {
				query: {
					materialCode: '',
					materialBatch: '',
					containerCode: '',
					quantity: ''
				},
				list: [],
				containerType: '',
				area: null,
				tips: {
					InboundOrderQty: 0,
					realQty: 0,
					targetStorage: null
				},
				show: false,
				title:'提示',
				content:'uView的目标是成为uni-app生态最优秀的UI框架',
				table: [],
				tableData: []
			};
		},
		onLoad(options) {
			uni.$on('scancodedate',(data) => this.getCode(data.code))
		},
		onUnload() {  
		  // 移除监听事件      
		  uni.$off('scancodedate')
		},
		methods: {
			// 扫码
			getCode(code) {
			},
			submit() {
				this.bind()
			},
			reset() {
				this.tableData = []
				this.query = {
					materialCode: '',
					materialBatch: '',
					containerCode: '',
					quantity: ''
				}
				this.tips = {
					InboundOrderQty: 0,
					realQty: 0,
					targetStorage: null
				}
				this.area = null
				this.containerType = ''
				this.show = false
			},
			// 容器位置区分
			getArea(value) {
				switch(value) {
					case 'L':
						return '箱库'
					case 'M':
						return '托盘库'
					default:
						return '无'
				}
			},
			async getMoreInfo() {
				const res = await getMoreInfoAPI(this.query)
				if (res.meta.code === 200) {
					this.containerType = res.data.containerType
					this.area = res.data.area
					this.tips.InboundOrderQty = res.data.InboundOrderQty
					this.tips.realQty = res.data.realQty
				}
			},
			async bind() {
				const res = await bindAPI({
					materialCode: this.query.materialCode,
					materialBatch: this.query.materialBatch,
					containerCode: this.query.containerCode,
					quantity: this.query.quantity,
					targetStorageCode: this.tips.targetStorage,
					type: 2
				})
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: '绑定成功'
					})
					this.query = {
						materialCode: '',
						materialBatch: '',
						containerCode: '',
						quantity: ''
					}
					this.tips = {
						InboundOrderQty: 0,
						realQty: 0,
						targetStorage: null
					}
					this.area = null
					this.containerType = ''
				} else {
					this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			async fullLoad() {
				if (!this.query.containerCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描容器码'
					})
				}
				const res = await fullLoadAPI(this.query.containerCode)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: '执行入库单成功'
					})
					this.query = {
						materialCode: '',
						materialBatch: '',
						containerCode: '',
						quantity: ''
					}
					this.tips = {
						InboundOrderQty: 0,
						realQty: 0,
						targetStorage: null
					}
					this.area = null
					this.containerType = ''
				} else if (res.meta.code === 201) {
					this.content = res.meta.msg
					this.show = true
				} else {
					this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			async getDetail() {
				const res = await getDetailAPI(this.query)
				if (res.meta.code === 200) {
					this.tableData = res.data.containerResp.containerDetails
					const result = this.tableData.every(item => item.preQty === 0)
					if (result) {
						this.$refs.uToast.show({
							type: 'warning',
							message: '该容器暂无预分配物料'
						})
					}
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			async useNullPallet() {
				if (!this.query.containerCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描容器码'
					})
				}
				const res = await useNullPalletAPI(this.query.containerCode)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.getNullPallet()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
